home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Smartcard 225877112001.psc / Module1.bas < prev   
Encoding:
BASIC Source File  |  2001-07-12  |  23.1 KB  |  926 lines

  1. Attribute VB_Name = "Module1"
  2. Option Explicit
  3. Public Sub PreScan()
  4. Dim hFile As Long, retVal As Long
  5. Dim sRegMonClass As String, sFileMonClass As String
  6.     
  7. 'This can be used to detect if SoftIce(tm)or RegMon(tm) is running
  8. sRegMonClass = Chr(82) & Chr(101) & Chr(103) & Chr(77) & Chr(111) & Chr(110) & Chr(67) & Chr(108) & Chr(97) & Chr(115) & Chr(115)
  9. sFileMonClass = Chr(70) & Chr(105) & Chr(108) & Chr(101) & Chr(77) & Chr(111) & Chr(110) & Chr(67) & Chr(108) & Chr(97) & Chr(115) & Chr(115)
  10.  
  11.  Select Case True
  12.    Case FindWindow(sRegMonClass, vbNullString) <> 0
  13.     End
  14.    Case FindWindow(sFileMonClass, vbNullString) <> 0
  15.     End
  16.  End Select
  17.  
  18. hFile = CreateFile("\\.\SICE", GENERIC_WRITE Or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  19. If hFile <> -1 Then
  20.    End
  21. Else
  22.  hFile = CreateFile("\\.\NTICE", GENERIC_WRITE Or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  23.  If hFile <> -1 Then
  24.     End
  25.  End If
  26. End If
  27.  
  28. End Sub
  29.                 
  30. Public Sub ResetForWrite()
  31.  
  32. Call CloseCOMM 'make sure port is closed if its open
  33.  
  34. 'open the port with windows API
  35. hPort = CreateFile(port, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
  36.  
  37. If hPort = INVALID_HANDLE_VALUE Then         'check if port opened ok
  38.   MsgBox port & " Error: invalid port or already in use."
  39.   Exit Sub
  40. End If
  41.  
  42. icond = SetupComm(hPort, 8192, 1024)         'set send/recv buffers
  43.  
  44. timeouts.ReadTotalTimeoutConstant = 10       'vital settings for smartcards
  45. timeouts.ReadTotalTimeoutMultiplier = 0
  46. timeouts.ReadIntervalTimeout = 0
  47. timeouts.WriteTotalTimeoutConstant = 100
  48. timeouts.WriteTotalTimeoutMultiplier = 0
  49.  
  50. icond = SetCommTimeouts(hPort, timeouts)     'enforce the settings
  51. icond = GetCommState(hPort, DCB)
  52.  
  53. SetDCBits fBinary, 1
  54. SetDCBits fParity, 0
  55. SetDCBits fOutxCtsFlow, 1
  56. SetDCBits fOutxDsrFlow, 1
  57. SetDCBits fDtrControl, DTR_CONTROL_ENABLE
  58. SetDCBits fDsrSensitivity, 1
  59. SetDCBits fTXContinueOnXoff, 0
  60. SetDCBits fOutX, 0
  61. SetDCBits fInX, 0
  62. SetDCBits fErrorChar, 0
  63. SetDCBits fNull, 0
  64. SetDCBits fDtrControl, DTR_CONTROL_HANDSHAKE
  65. SetDCBits fRtsControl, RTS_CONTROL_ENABLE
  66. SetDCBits fAbortOnError, 0
  67.  
  68. DCB.BaudRate = ResetBaud                   'set baud to atr speed 9600
  69. DCB.ByteSize = 8
  70. DCB.Parity = NOPARITY
  71. DCB.StopBits = ONESTOPBIT
  72.  
  73. icond = SetCommState(hPort, DCB)
  74. icond = SetCommTimeouts(hPort, timeouts)
  75.  
  76. icond = EscapeCommFunction(hPort, CLRRTS) 'heres where we call the atr
  77.  
  78. If icond = False Then
  79.   MsgBox "Error Setting COM State."
  80.   CloseHandle hPort
  81.   Exit Sub
  82. End If
  83.  
  84. 'read input BufferIn to get ATR data
  85. Call ReadATR
  86.  
  87. 'show the ATR data
  88. Call ShowATR
  89. DelaySecs 0.25
  90.  
  91. icond = SetupComm(hPort, 8192, 1024)       'if we got atr then set baud for data sends
  92. timeouts.ReadTotalTimeoutConstant = 10
  93. timeouts.ReadTotalTimeoutMultiplier = 0
  94. timeouts.ReadIntervalTimeout = 0
  95. timeouts.WriteTotalTimeoutConstant = 100
  96. timeouts.WriteTotalTimeoutMultiplier = 0
  97. icond = SetCommTimeouts(hPort, timeouts)
  98. icond = GetCommState(hPort, DCB)
  99.  
  100. SetDCBits fBinary, 1
  101. SetDCBits fParity, 0
  102. SetDCBits fOutxCtsFlow, 1
  103. SetDCBits fOutxDsrFlow, 1
  104. SetDCBits fDtrControl, DTR_CONTROL_ENABLE
  105. SetDCBits fDsrSensitivity, 1
  106. SetDCBits fTXContinueOnXoff, 0
  107. SetDCBits fOutX, 0
  108. SetDCBits fInX, 0
  109. SetDCBits fErrorChar, 0
  110. SetDCBits fNull, 0
  111. SetDCBits fDtrControl, DTR_CONTROL_HANDSHAKE
  112. SetDCBits fRtsControl, RTS_CONTROL_ENABLE
  113. SetDCBits fAbortOnError, 0
  114.  
  115. DCB.BaudRate = DataBaud
  116. DCB.ByteSize = 8
  117. DCB.Parity = ODDPARITY
  118. DCB.StopBits = TWOSTOPBITS
  119. DCB.DCBlength = Len(DCB)
  120.  
  121. icond = SetCommState(hPort, DCB)
  122. icond = SetCommTimeouts(hPort, timeouts)
  123. 'reinit the COM port
  124. icond = EscapeCommFunction(hPort, CLRRTS)
  125.  
  126. If icond = False Then
  127.   MsgBox "Error Setting Com State."
  128.   CloseHandle hPort
  129.   Exit Sub
  130. End If
  131.  
  132.  
  133. End Sub
  134.  
  135. Public Sub CheckCOM(xPort)
  136.  
  137. Form1.PORTLITE.Picture = Form1.PortOFF.Picture
  138. Call CloseCOMM  'make sure we close prev. comport first
  139.  
  140. 'open COM port for generic read/write
  141. hPort = CreateFile(port, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
  142.  
  143. If hPort = INVALID_HANDLE_VALUE Then
  144.    Form1.StatusLabel.Caption = " ACTION: " + port + " already in use or not available."
  145.    Form1.PORTLITE.Picture = Form1.PortOFF.Picture
  146.   Exit Sub
  147. Else
  148.   Form1.StatusLabel.Caption = " ACTION: " + port + " opened OK"
  149.   Form1.PORTLITE.Picture = Form1.PortON.Picture
  150. End If
  151.  
  152. End Sub
  153.  
  154. Public Sub ResetForATR()
  155. Dim f
  156.  
  157. Call CloseCOMM
  158.  
  159. 'open COM port for generic read/write
  160. hPort = CreateFile(port, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
  161.  
  162. If hPort = INVALID_HANDLE_VALUE Then
  163.   MsgBox port & " Error: invalid port or already in use."
  164.   Exit Sub
  165. End If
  166.  
  167. icond = SetupComm(hPort, 8192, 1024)
  168. timeouts.ReadTotalTimeoutConstant = 10
  169. timeouts.ReadTotalTimeoutMultiplier = 0
  170. timeouts.ReadIntervalTimeout = 0
  171. timeouts.WriteTotalTimeoutConstant = 100
  172. timeouts.WriteTotalTimeoutMultiplier = 0
  173.  
  174. icond = SetCommTimeouts(hPort, timeouts)
  175. icond = GetCommState(hPort, DCB)
  176.  
  177. SetDCBits fBinary, 1
  178. SetDCBits fParity, 0
  179. SetDCBits fOutxCtsFlow, 1
  180. SetDCBits fOutxDsrFlow, 1
  181. SetDCBits fDtrControl, DTR_CONTROL_ENABLE
  182. SetDCBits fDsrSensitivity, 1
  183. SetDCBits fTXContinueOnXoff, 0
  184. SetDCBits fOutX, 0
  185. SetDCBits fInX, 0
  186. SetDCBits fErrorChar, 0
  187. SetDCBits fNull, 0
  188. SetDCBits fDtrControl, DTR_CONTROL_HANDSHAKE
  189. SetDCBits fRtsControl, RTS_CONTROL_ENABLE
  190. SetDCBits fAbortOnError, 0
  191.  
  192. DCB.BaudRate = ResetBaud
  193. DCB.ByteSize = 8
  194. DCB.Parity = NOPARITY
  195. DCB.StopBits = ONESTOPBIT
  196.  
  197. icond = SetCommState(hPort, DCB)
  198. icond = SetCommTimeouts(hPort, timeouts)
  199.  
  200. 'init the ATR by dropping RTS
  201. icond = EscapeCommFunction(hPort, CLRRTS)
  202.  
  203. If icond = False Then
  204.   MsgBox "Error Setting COM State."
  205.   CloseHandle hPort
  206.   Exit Sub
  207. End If
  208.  
  209. 'read input BufferIn to get ATR data
  210. Call ReadATR
  211.  
  212. 'show the ATR data
  213. Call ShowATR
  214. DelaySecs 0.25
  215.  
  216. 'flush the BufferIn
  217. icond = PurgeComm(hPort, PURGE_RXCLEAR Or PURGE_TXCLEAR)
  218.  
  219.  
  220. End Sub
  221.  
  222. Public Sub WriteCOMM(DATA As String)
  223.  
  224. 'you can send a formatted data byte here
  225. icond = WriteFile(hPort, DATA, 1, written, 0)
  226. InverseBuffer = ""
  227.  
  228. End Sub
  229.  
  230. Public Sub ReadATR()
  231.  
  232. BufferIncount = 0
  233. BufferIn = ""
  234. DelaySecs 0.15
  235.  
  236. 'loop thru and read inBuffer
  237. Do
  238. icond = ReadFile(hPort, InBuff, 1, numRead, 0)
  239. If numRead = 0 Then Exit Sub
  240.  BufferIn = BufferIn & InBuff
  241.  BufferIncount = Len(BufferIn)
  242. Loop
  243.  
  244. End Sub
  245.  
  246. Public Sub ReadDATA()
  247.  
  248. Form1.RXLITE.Picture = Form1.RXOFF.Picture
  249.  
  250. If Len(BufferIn) >= 1 Then
  251.    If Len(BufferIn) > 3 Then CardInserted = True: Exit Sub
  252.    BufferIn = "" 'clear out possible trash
  253. End If
  254.  
  255. Do
  256. Form1.RXLITE.Picture = Form1.RXON.Picture: DoEvents: DoEvents
  257. icond = ReadFile(hPort, InBuff, 1, numRead, 0)
  258. If numRead = 0 Then Form1.RXLITE.Picture = Form1.RXOFF.Picture: Exit Sub
  259.  BufferIn = BufferIn & InBuff
  260.  BufferIncount = Len(BufferIn)
  261.  Form1.BuffCntText = BufferIncount
  262. Loop
  263.  
  264. End Sub
  265.  
  266. Public Sub CloseCOMM()
  267.  
  268. If hPort = 0 Or hPort = INVALID_HANDLE_VALUE Then Exit Sub
  269.  icond = EscapeCommFunction(hPort, CLRDTR)
  270.  CloseHandle (hPort)
  271.  
  272. End Sub
  273.  
  274. Public Sub SetDCBits(Pos As Long, val As Integer)
  275. Dim ip As Integer
  276. Dim imul As Long
  277. Dim poz As Long
  278.  
  279. imul = 1
  280. poz = Pos
  281. For ip = 1 To 32
  282. If (poz And 1) Then Exit For
  283. poz = poz / 2
  284. imul = imul * 2
  285. Next
  286.  
  287.  DCB.Bits1 = DCB.Bits1 And (Not Pos) Or (imul * val)
  288.  
  289. End Sub
  290.  
  291. Public Sub DelaySecs(ByVal seconds As Single)
  292. Static start As Single
  293.  
  294. start = Timer
  295. Do While Timer < start + seconds
  296.   DoEvents
  297. Loop
  298.  
  299. End Sub
  300.  
  301. Public Sub Inverse(databyte As String)
  302. Dim Cpos As Integer
  303. Dim xxx As Integer
  304. Dim RealByte
  305. Dim TmpBuffer As String
  306.  
  307.  Nibble = 1
  308.  RealByte = 0
  309.          
  310.  For Npos = 1 To Len(databyte)
  311.      Temp3 = Mid$(databyte, Npos, 1)
  312.             
  313. If Nibble >= 1 Then
  314.   
  315. Select Case Temp3
  316.    Case "0" To "9"
  317.       RealByte = RealByte + (val(Temp3) * 16)
  318.       Nibble = 0
  319.    Case "A" To "F"
  320.       RealByte = RealByte + ((Asc(Temp3) - 55) * 16)
  321.       Nibble = 0
  322.   End Select
  323.  
  324. Else
  325.  
  326. Select Case Temp3
  327.    Case "0" To "9"
  328.       RealByte = RealByte + (val(Temp3))
  329.    Case "A" To "F"
  330.       RealByte = RealByte + (Asc(Temp3) - 55)
  331.   End Select
  332.       
  333. End If
  334.     
  335.     Next Npos
  336.        
  337.    temp1 = (RealByte Xor 255)
  338.    RealByte = temp1
  339.    Temp3 = 0
  340.         
  341. For Cpos = 7 To 0 Step -1
  342.    Select Case Cpos
  343.      Case 7:  Temp2 = RealByte And 1
  344.      Case 6:  Temp2 = RealByte And 2
  345.      Case 5:  Temp2 = RealByte And 4
  346.      Case 4:  Temp2 = RealByte And 8
  347.      Case 3:  Temp2 = RealByte And 16
  348.      Case 2:  Temp2 = RealByte And 32
  349.      Case 1:  Temp2 = RealByte And 64
  350.      Case 0:  Temp2 = RealByte And 128
  351.    End Select
  352.      
  353. If Temp2 > 0 Then
  354.   If (Cpos = 0) Then
  355.       Temp3 = Temp3 + 1
  356.  Else
  357.       Temp3 = Temp3 + (2 ^ Cpos)
  358.   End If
  359. Else
  360.       Temp3 = Temp3
  361.      End If
  362.   
  363. Next Cpos
  364.  
  365.    InverseBuffer = Temp3
  366.  
  367. End Sub
  368.  
  369. Public Sub CardInfo2A(theString As String)
  370. Dim ret, zzz, ttt, ooo As Integer
  371. Dim TempBuf As String
  372. Dim CardIDlong
  373.  
  374. 'clear all the variables first
  375. TempBuf = ""
  376. CardIDlong = 0
  377. ret = 0
  378. zzz = 0
  379. ttt = 0
  380. ooo = 0
  381. tmpCARDID = ""
  382. tmpIRD = ""
  383. tmpUSW = ""
  384. tmpGUIDE = ""
  385. tmpRATING = ""
  386. tmpSPENDING = ""
  387.  
  388.  
  389. ooo = Len(theString)
  390.  
  391. For ret = 1 To ooo / 2
  392.              
  393.     On Error Resume Next
  394.      TempBuf = Left(theString, 2)
  395.      If Trim(TempBuf) = "" Then Exit For
  396.      zzz = InStr(1, theString, Len(TempBuf))
  397.      theString = Mid(theString, 3, Len(theString))
  398.      
  399.      If ret = 9 Then
  400.         tmpFUSE = TempBuf
  401.         GoTo NEGST
  402.      End If
  403.      
  404.      If ret = 11 Then
  405.         tmpRATING = TempBuf
  406.         GoTo NEGST
  407.      End If
  408.      
  409.      If ret = 12 Or ret = 13 Then
  410.        If ret = 12 Then tmpSPENDING = TempBuf: GoTo NEGST
  411.         If ret = 13 Then
  412.           tmpSPENDING = tmpSPENDING + TempBuf
  413.           tmpSPENDING = val("&H" + tmpSPENDING)
  414.           If tmpSPENDING <> 0 Then
  415.              tmpTRASH = Left(tmpSPENDING, Len(tmpSPENDING) - 2)
  416.           Else
  417.              tmpTRASH = Left(tmpSPENDING, Len(tmpSPENDING))
  418.           End If
  419.           If tmpTRASH = 0 Then
  420.              tmpSPENDING = "$0.00"
  421.           Else
  422.              tmpSPENDING = "$" + tmpTRASH + "." + Right(tmpSPENDING, 2)
  423.           End If
  424.          GoTo NEGST
  425.         End If
  426.      End If
  427.      
  428.      If ret = 21 Or ret = 22 Or ret = 23 Or ret = 24 Then
  429.       If ret = 21 Then tmpCARDID = TempBuf: GoTo NEGST
  430.        If ret = 22 Then tmpCARDID = tmpCARDID + TempBuf: GoTo NEGST
  431.         If ret = 23 Then tmpCARDID = tmpCARDID + TempBuf: GoTo NEGST
  432.          If ret = 24 Then
  433.           tmpCARDID = tmpCARDID + TempBuf
  434.           CardIDlong = tmpCARDID
  435.           CardIDlong = val("&H" + CardIDlong)
  436.           tmpCARDID = CardIDlong
  437.           tmpCARDID = "000" & tmpCARDID & "_"
  438.         GoTo NEGST
  439.        End If
  440.      End If
  441.      
  442.      If ret = 25 Or ret = 26 Or ret = 27 Or ret = 28 Then
  443.       If ret = 25 Then tmpIRD = TempBuf: GoTo NEGST
  444.        If ret = 26 Then tmpIRD = tmpIRD + TempBuf: GoTo NEGST
  445.         If ret = 27 Then tmpIRD = tmpIRD + TempBuf: GoTo NEGST
  446.          If ret = 28 Then
  447.           tmpIRD = tmpIRD + TempBuf
  448.           tmpIRD = val("&H" + tmpIRD)
  449.           tmpIRD = CardIDlong Xor tmpIRD
  450.           tmpIRD = Hex(val(tmpIRD))
  451.           If tmpIRD = 0 Then tmpIRD = "00000000"
  452.         GoTo NEGST
  453.        End If
  454.      End If
  455.      
  456.       If ret = 30 Then
  457.         tmpUSW = TempBuf
  458.         tmpUSW = val("&H" + tmpUSW)
  459.      End If
  460.      
  461. NEGST:
  462.      
  463.  Next ret
  464.  
  465. CardInfoBuffer = ""                 'clear this buffer for 58 cmd
  466.  
  467. Exit Sub
  468.  
  469. ERRORED:
  470.   MsgBox "We Hit an error at ret = " & ret
  471.       
  472. End Sub
  473.  
  474. Public Sub CardInfo58(theString As String)
  475. Dim ret, zzz, ttt, ooo As Integer
  476. Dim TempBuf As String
  477.  
  478. ooo = Len(theString)
  479.  
  480. For ret = 1 To ooo / 2
  481.              
  482.     On Error Resume Next
  483.      TempBuf = Left(theString, 2)
  484.      If Trim(TempBuf) = "" Then Exit For
  485.      zzz = InStr(1, theString, Len(TempBuf))
  486.      theString = Mid(theString, 3, Len(theString))
  487.      
  488.     If ret = 11 Then
  489.         tmpTIMEZONE = TempBuf
  490.        GoTo NEGST
  491.      End If
  492.      
  493.      If ret = 13 Then
  494.         tmpGUIDE = TempBuf
  495.        GoTo NEGST
  496.      End If
  497.  
  498. NEGST:
  499.      
  500.  Next ret
  501.  
  502. CardInfoBuffer = ""                 'clear this buffer
  503.  
  504. 'SHOW EM ALL HERE!!!
  505. Form1.CardIDtext = tmpCARDID
  506. Form1.IRDText = tmpIRD
  507. Form1.USWtext = tmpUSW
  508. Form1.FUSEtext = tmpFUSE
  509. Form1.GUIDEtext = tmpGUIDE
  510. Form1.TIMEZONEtext = tmpTIMEZONE
  511. Form1.RATINGtext = tmpRATING
  512. Form1.SPENDINGLIMITtext = tmpSPENDING
  513.  
  514. Exit Sub
  515.  
  516. ERRORED:
  517.   MsgBox "We Hit an error at ret = " & ret
  518.  
  519. End Sub
  520.  
  521. Public Sub CardInfoPPV(theString As String)
  522. Dim ret, zzz, xxx, ooo As Integer
  523. Dim TempBuf As String
  524. Dim PPVStr
  525.  
  526. 'clear the ppv strings
  527. For xxx = 1 To 25
  528.    PPV(xxx) = ""
  529. Next xxx
  530.  
  531. ooo = Len(theString)
  532.  
  533. 'load the ppv strings we are read
  534. xxx = 0
  535. For ret = 1 To ooo / 2
  536.              
  537.     On Error Resume Next
  538.      xxx = ret
  539.      TempBuf = Left(theString, 2)
  540.      If Trim(TempBuf) = "" Then Exit For
  541.      zzz = InStr(1, theString, Len(TempBuf))
  542.      theString = Mid(theString, 3, Len(theString))
  543.      
  544. Select Case ret
  545.    Case Is = 1, 2, 3: PPV(&H1) = PPV(&H1) + TempBuf
  546.    Case Is = 4, 5, 6: PPV(&H2) = PPV(&H2) + TempBuf
  547.    Case Is = 7, 8, 9: PPV(&H3) = PPV(&H3) + TempBuf
  548.    Case Is = 10, 11, 12: PPV(4) = PPV(4) + TempBuf
  549.    Case Is = 13, 14, 15: PPV(5) = PPV(5) + TempBuf
  550.    Case Is = 16, 17, 18: PPV(6) = PPV(6) + TempBuf
  551.    Case Is = 19, 20, 21: PPV(7) = PPV(7) + TempBuf
  552.    Case Is = 22, 23, 24: PPV(8) = PPV(8) + TempBuf
  553.    Case Is = 25, 26, 27: PPV(9) = PPV(9) + TempBuf
  554.    Case Is = 28, 29, 30: PPV(10) = PPV(10) + TempBuf
  555.    Case Is = 31, 32, 33: PPV(11) = PPV(11) + TempBuf
  556.    Case Is = 34, 35, 36: PPV(12) = PPV(12) + TempBuf
  557.    Case Is = 37, 38, 39: PPV(13) = PPV(13) + TempBuf
  558.    Case Is = 40, 41, 42: PPV(14) = PPV(14) + TempBuf
  559.    Case Is = 43, 44, 45: PPV(15) = PPV(15) + TempBuf
  560.    Case Is = 46, 47, 48: PPV(16) = PPV(16) + TempBuf
  561.    Case Is = 49, 50, 51: PPV(17) = PPV(17) + TempBuf
  562.    Case Is = 52, 53, 54: PPV(18) = PPV(18) + TempBuf
  563.    Case Is = 55, 56, 57: PPV(19) = PPV(19) + TempBuf
  564.    Case Is = 58, 59, 60: PPV(20) = PPV(20) + TempBuf
  565.    Case Is = 61, 62, 63: PPV(21) = PPV(21) + TempBuf
  566.    Case Is = 64, 65, 66: PPV(22) = PPV(22) + TempBuf
  567.    Case Is = 67, 68, 69: PPV(23) = PPV(23) + TempBuf
  568.    Case Is = 70, 71, 72: PPV(24) = PPV(24) + TempBuf
  569.    Case Is = 73, 74, 75: PPV(25) = PPV(25) + TempBuf
  570.     Case Else
  571.        GoTo NEGST
  572.     End Select
  573.     
  574. NEGST:
  575.      
  576.  Next ret
  577.  
  578. CardInfoBuffer = ""                 'clear this buffer
  579.  
  580. xxx = 0
  581.  
  582. Exit Sub
  583.  
  584. ERRORED:
  585.   MsgBox "We Hit an error at ret = " & ret
  586.  
  587.  
  588. End Sub
  589. 'call this 1st thing in sub ShowDATA
  590. Public Sub FlipBuffer()
  591. Dim ret, zzz, ttt, ooo As Integer
  592. Dim FlipTemp As String * 3
  593. Dim TempBuf As String
  594. Dim BufLen As Integer
  595.  
  596. R0byte = ""
  597.  
  598. BufLen = Len(BufferIn)
  599.  
  600. For ret = 1 To BufLen  '(BufferIn)
  601.              
  602.     On Error Resume Next
  603.      TempBuf = Left(BufferIn, 1)
  604.      If Trim(TempBuf) = "" Then Exit For
  605.      zzz = InStr(1, BufferIn, TempBuf)
  606.      BufferIn = Mid(BufferIn, zzz + 1, Len(BufferIn))
  607.      TempBuf = Hex(Asc(BufferIn))
  608.       
  609.      If Len(TempBuf) = 1 Then
  610.        TempBuf = "0" + TempBuf
  611.      End If
  612.        
  613.     Call Inverse(TempBuf)
  614.        
  615.     TempBuf = Hex(InverseBuffer)
  616.      
  617.      If Len(TempBuf) = 1 Then
  618.         TempBuf = "0" + TempBuf
  619.      End If
  620.      
  621.      ByteToFlip(ret) = TempBuf
  622.      
  623.     If ret = BufLen - 2 Then
  624.       R0byte = ByteToFlip(ret)
  625.     End If
  626.     
  627.     If ret = BufLen - 1 Then
  628.       R0byte = R0byte + " " + ByteToFlip(ret)
  629.       BufferIn = ""
  630.       GoTo FIN                                   'we know this is last byte sent from
  631.      End If                                      'card so clear any trash and exit
  632.      
  633.  Next ret
  634.  
  635. FIN:
  636.      BytesToRead = ret
  637.  
  638. Exit Sub
  639.  
  640. ERRORED:
  641.   MsgBox "We Hit an error at ret = " & ret
  642.   BufferIn = ""
  643.       
  644.  
  645. End Sub
  646.  
  647. Public Sub SendData(StrName As String)
  648. Dim yyy As Integer
  649. Dim zzz As Integer
  650. Dim num As Integer
  651.  
  652. 'clear any trash data that may be present
  653. PurgeComm hPort, PURGE_RXCLEAR Or PURGE_TXCLEAR
  654.  
  655. Form1.TXLITE.Picture = Form1.TXOFF.Picture: DoEvents
  656.  
  657. 'make sure atr was at least right length
  658. If AtrLen < 38 Then Exit Sub
  659.  
  660.  
  661. num = Len(StrName) / 2                               '# of bytes for this data string
  662.  
  663. xxx = 0                                                'set xxx to 0
  664.                     
  665. For xxx = 1 To num
  666.    
  667.    'keep this variable clean
  668.     SendStr(xxx) = ""
  669.    
  670.    If xxx = 1 Then
  671.      If Trim(StrName) = "" Then Exit For             'if data string empty then stop
  672.      ByteStr$ = Trim$(Left(StrName, 2))              'grab 2 bytes from data string
  673.      zzz = InStr(1, StrName, ByteStr$)                 'set len of data to minus 1st 2 bytes
  674.      StrName = Mid(StrName, zzz + 2, Len(StrName))     'remove the 2 bytes from orig string
  675.    Else
  676.      If Trim(StrName) = "" Then Exit For             'if data string empty then stop
  677.      ByteStr$ = Trim$(Left(StrName, 2))              'grab 2 bytes from data string
  678.      zzz = InStr(1, StrName, ByteStr$)                 'set len of data to -2 1st places
  679.      StrName = Mid(StrName, zzz + 2, Len(StrName))     'remove the 2 bytes from orig string
  680.    End If
  681.   
  682.    'make sure our hex string is 2 bytes long
  683.    Call CheckHexLen(ByteStr$, 1)
  684.    
  685.    'add the hex byte to array
  686.    SendStr(xxx) = ByteStr$                             'set the array for the data/header bytes
  687.    
  688. Next xxx
  689.  
  690.  
  691.  xxx = 0                                               'clear xxx again
  692.  
  693.  
  694. For xxx = 1 To num
  695.     
  696.     Form1.TXLITE.Picture = Form1.TXOFF.Picture: DoEvents
  697.     
  698.     'flip the bits/bytes
  699.     Call Inverse(SendStr(xxx))
  700.    
  701.     If Trim(StrName) = "" Then
  702.      TmpStr$ = Chr(InverseBuffer)                    'format the data
  703.     Else
  704.      MsgBox "Data in header or packet did not parse correctly!", 0, "ERROR"
  705.      Exit Sub
  706.     End If
  707.     
  708.     Form1.TXLITE.Picture = Form1.TXON.Picture: DoEvents
  709.     WriteFile hPort, TmpStr$, 1, written, 0         'write the byte to card
  710.     DelaySecs (0.015)
  711.     ReadFile hPort, InBuff, 1, numRead, 0           'read the echo bytes
  712.     BytesTotalSent = BytesTotalSent + 1
  713.     Form1.BYTESsentText = Str(BytesTotalSent)
  714.     
  715.     If CheckINS Then
  716. '      Stop
  717.       DoEvents
  718.       If InBuff = TmpStr Then
  719.        DoEvents
  720.       Else
  721.        MsgBox "Byte did not echo correctly!"
  722. '      Stop
  723.        Call CloseCOMM
  724.        End
  725.        Exit Sub
  726.       End If
  727.     End If
  728. Next
  729.  
  730.     Form1.TXLITE.Picture = Form1.TXOFF.Picture: DoEvents
  731.  
  732. End Sub
  733.  
  734. Public Sub CheckHexLen(dxStr As String, xNum As Integer)
  735.  
  736. Select Case xNum
  737. Case Is = 1: If Len(dxStr) = 1 Then ByteStr = "0" + ByteStr
  738. Case Is = 2: If Len(dxStr) = 1 Then WorkByte = "0" + WorkByte
  739. Case Is = 3: If Len(dxStr) = 1 Then preDATA = "0" + preDATA
  740. Case Is = 4: If Len(PreATR) = 1 Then PreATR = "0" + PreATR
  741. End Select
  742.  
  743. End Sub
  744. Public Sub GetReturn()
  745.   
  746.   'this sub gets the INS(42,40..etc)
  747.   'and 90 00 / 90 80 type responses after sending data
  748.   
  749.   postDATA = "": preDATA = ""
  750.   WorkByte = "":  R0byte = ""
  751.   
  752.   Call ReadDATA
  753.   
  754.   For xxx = 1 To Len(BufferIn)
  755.       
  756.       WorkByte = Hex(Asc(Mid(BufferIn, xxx, 1)))
  757.    
  758.       Call CheckHexLen(WorkByte, 2)
  759.       
  760.       Call Inverse(WorkByte)
  761.       
  762.       preDATA = Hex(InverseBuffer$)
  763.       
  764.       Call CheckHexLen(preDATA, 3)
  765.       
  766.       postDATA = postDATA + " " + preDATA
  767.  
  768.   Next
  769.   
  770.       R0byte = LTrim(RTrim(postDATA))
  771.       Form1.R02Label.Text = R0byte
  772.       
  773. End Sub
  774.  
  775. Public Sub ShowATR()
  776.   
  777.   ATR$ = ""
  778.   PreATR = ""
  779.   PostATR = ""
  780.   
  781.   For xxx = 1 To Len(BufferIn)
  782.      WorkByte = Hex(Asc(Mid(BufferIn, xxx, 1))) 'grab 1 byte from BufferIn
  783.       
  784.       Call CheckHexLen(WorkByte, 2)
  785.           
  786.       Call Inverse(WorkByte)                'reverse and invert it
  787.       
  788.       PreATR = Hex(InverseBuffer)           'convert to Hex
  789.           
  790.       Call CheckHexLen(PreATR, 4)
  791.       
  792.       PostATR = PostATR + " " + PreATR       'add each hex byte to holder string
  793.       ATR$ = Trim$(PostATR)                  'trim off any end spaces if any
  794.       
  795.    Next xxx
  796.    
  797.    
  798.  Select Case Mid(ATR$, 1, 5)
  799.   Case Is = "3F 7F"
  800.     Form1.ATRlabel.Caption = ""
  801.     Form1.ATRlabel.Caption = " ATR:    " + ATR$
  802.     Form1.StatusLabel.Caption = " ACTION: HU series (P3) ATR detected"
  803.     AtrLen = Len(ATR): BufferIn = "": ATR$ = ""
  804.     
  805.   Case Is = "3F 78"
  806.     Form1.ATRlabel.Caption = ""
  807.     Form1.ATRlabel.Caption = " ATR:    " + ATR$
  808.     Form1.StatusLabel.Caption = " ACTION:  H series (P2) ATR detected"
  809.     AtrLen = Len(ATR): BufferIn = "": ATR$ = ""
  810.     
  811.    Case Is = "3F 76"
  812.     Form1.ATRlabel.Caption = ""
  813.     Form1.ATRlabel.Caption = " ATR:    " + ATR$
  814.     Form1.StatusLabel.Caption = " ACTION:  F series (P1) ATR detected"
  815.     AtrLen = Len(ATR): BufferIn = "": ATR$ = ""
  816.     
  817.    Case Else
  818.     MsgBox "Unknown or corrupt ATR"
  819.     Form1.ATRlabel.Caption = ""
  820.     Form1.ATRlabel.Caption = " ATR:    " + ATR$
  821.     Form1.StatusLabel.Caption = " ACTION:  ? series (P?) Unknown/No ATR"
  822.     AtrLen = Len(ATR): BufferIn = "": ATR$ = ""
  823.  End Select
  824.     
  825.     
  826. End Sub
  827.  
  828. Public Sub ShowDATA()
  829.  
  830. 'convert it to hex so we can parse it below
  831.  Call FlipBuffer
  832.  
  833. 'clear out our variables
  834.   WorkByte = ""
  835.   preDATA = ""
  836.   postDATA = ""
  837.   
  838. 'parse the coverted Hex into an array
  839.   For xxx = 1 To BytesToRead
  840.      WorkByte = ByteToFlip(xxx)
  841.      CardInfoBuffer = CardInfoBuffer + WorkByte
  842.      postDATA = WorkByte
  843.      
  844.     If xxx = BytesToRead - 1 Then
  845.       R0byte = postDATA
  846.       GoTo skip
  847.     End If
  848.      
  849.     If xxx = BytesToRead Then
  850.       R0byte = R0byte + " " + postDATA
  851.       GoTo skip
  852.     End If
  853.      
  854.     DATA$ = DATA$ + " " + Trim$(postDATA)
  855.  
  856. skip:
  857.   
  858.   Next xxx
  859.    
  860.      Form1.R02Label.Text = R0byte
  861.      Form1.txtOut.Text = Trim(DATA$)
  862.      CardInfoBuffer = Trim(CardInfoBuffer)
  863.      DATA = ""
  864.  
  865. End Sub
  866.  
  867. Public Sub ClearVariables()
  868.  
  869.  Form1.txtOut.Text = "":
  870.  Form1.TextInReadBuffer.Text = ""
  871.  Form1.CardIDtext.Text = ""
  872.  Form1.IRDText.Text = ""
  873.  Form1.USWtext.Text = ""
  874.  Form1.FUSEtext.Text = ""
  875.  Form1.GUIDEtext.Text = ""
  876.  Form1.TIMEZONEtext.Text = ""
  877.  Form1.RATINGtext.Text = ""
  878.  Form1.SPENDINGLIMITtext.Text = ""
  879.  Form1.BuffCntText.Text = ""
  880.  Form1.R02Label.Text = ""
  881.  Form1.BYTESsentText.Text = ""
  882.  ATR = "":
  883.  PreATR = "":
  884.  PostATR = "":
  885.  DATA = "":
  886.  preDATA = "":
  887.  postDATA = "":
  888.  BufferIn = "":
  889.  BytesTotalSent = 0
  890.  
  891. End Sub
  892.  
  893. Public Sub ToggleButtons()
  894.  
  895. If Form1.CARDinfoBtn.Enabled = True Then
  896.   Form1.ATRlabel.Caption = ""
  897.   Form1.COMMlist.Enabled = False
  898.   Form1.CARDinfoBtn.Enabled = False
  899. Else
  900.   Form1.CARDinfoBtn.Enabled = True
  901.   Form1.COMMlist.Enabled = True
  902. End If
  903.  
  904. End Sub
  905.  
  906. Public Sub SaveState()
  907.  
  908. 'save the port user is using so he dont have to select it every time
  909. SaveSetting "HKEY_CLASSES_ROOT\Interface\{F9043C87-F6F2-101A-A3C9-08002B2F49FF}\TypeLib", "Properties", "Port", port  'save COM# to registry
  910.     
  911. End Sub
  912.  
  913. Public Sub GetState()
  914.  
  915. 'get the port user is using from the last time
  916. port = GetSetting("HKEY_CLASSES_ROOT\Interface\{F9043C87-F6F2-101A-A3C9-08002B2F49FF}\TypeLib", "Properties", "Port", "")
  917.  
  918. End Sub
  919.  
  920. Public Sub ShowStatus(xMsg)
  921.  
  922. xMsg = " ACTION: " + xMsg
  923. Form1.StatusLabel.Caption = xMsg
  924.  
  925. End Sub
  926.